package net.neiquan.neiquandemowithkf.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.neiquan.neiquandemowithkf.R;
import net.neiquan.neiquandemowithkf.activity.ContainerActivity;
import net.neiquan.neiquandemowithkf.activity.MainActivity;
import net.neiquan.neiquandemowithkf.application.MyApplication;
import net.neiquan.neiquandemowithkf.entity.User;
import net.neiquan.neiquandemowithkf.utils.Tools;
import net.neiquanlib.base.AppBaseFragment;
import net.neiquanlib.dialog.AlertDialog;
import net.neiquanlib.inter.UserEvent;
import net.neiquanlib.utils.ImageUtils;
import net.neiquanlib.widget.RoundImageView;

import org.greenrobot.eventbus.EventBus;
import org.haitao.common.utils.AppLog;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * 作者 : hjb
 * 时间 : 2016/4/25.
 */
public class TabFourFragment extends AppBaseFragment {
    @InjectView(R.id.text1)
    TextView mText1;
    @InjectView(R.id.text2)
    TextView mText2;
    @InjectView(R.id.text3)
    TextView mText3;
    @InjectView(R.id.root_Ll)
    LinearLayout mRootLl;
    @InjectView(R.id.head_img)
    RoundImageView mHeadImg;
    @InjectView(R.id.nickName)
    TextView mNickName;
    @InjectView(R.id.login_out_btn)
    Button mLoginOutBtn;
    @InjectView(R.id.background)
    ImageView mbackground;

    boolean isLogin = false;
    private String headUrl;

    @Override
    public View initRootView() {
        rootView = mInflater.inflate(R.layout.frag_mine, null);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    private boolean isLogin() {
        setUserInfo();
        if (!MyApplication.getInstance().isLogin()) {
            mLoginOutBtn.setText("登陆");
            isLogin = false;
        } else {
            mLoginOutBtn.setText("退出登陆");
            isLogin = true;
        }
        return isLogin;
    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin();
    }

    @Override
    public void setViews() {
        setBackGone();
        setTitleTv("我的");
    }

    @Override
    public void initData() {
        isLogin();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onEventMainThread(UserEvent event) {
        super.onEventMainThread(event);
        if (event.getState() == UserEvent.UPDATE_INFO) {
            Toast.makeText(getActivity(), "登陆了", Toast.LENGTH_SHORT).show();
            isLogin();
        }
    }

    private void setUserInfo() {
        User mUser = MyApplication.getInstance().mUser;
        mHeadImg.setImageResource(R.drawable.pictures_no2);
        mNickName.setText("");
        if (mUser != null) {
            if (!TextUtils.isEmpty(mUser.getHeadUrl())) {
                headUrl = mUser.getHeadUrl();
                ImageUtils.loadPicNet(headUrl, mHeadImg);
                Tools.imageToBlur(headUrl, mbackground);
            }
            mNickName.setText(!TextUtils.isEmpty(mUser.getNickName()) ? mUser.getNickName() : "");
        }
    }

    @OnClick({R.id.button1, R.id.button2, R.id.button3, R.id.head_img, R.id.login_out_btn, R.id.editInfo})
    public void onClick(View view) {
        if (!isLogin()) {
            AppLog.e("没有登陆");
            ContainerActivity.startActivity(getActivity(), LoginFragment.class, null);
            return;
        }
        switch (view.getId()) {
            case R.id.button1://切换到首页
                ((MainActivity) activity).switchFragment(0);
                break;
            case R.id.button2:
                ContainerActivity.startActivityForResult(getActivity(), OneFragment.class, null, 12);
                break;
            case R.id.button3:
                ContainerActivity.startActivity(getActivity(), OneFragment.class, null);
                break;
            case R.id.head_img:
                if (headUrl != null) {
                    Tools.previewSingle(headUrl);
                }
                break;
            case R.id.login_out_btn:
                new AlertDialog.Builder(getActivity())
                        .setTitle("退出登陆")
                        .setMessage("确认退出?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyApplication.getInstance().loginOut();
                                isLogin();
                            }
                        }).setNegativeButton("取消", null)
                        .create()
                        .show();
                break;
            case R.id.editInfo:
                ContainerActivity.startActivity(getActivity(), ModifyInfoFragment.class, null);
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String string = data.getStringExtra("string");
        Log.i("----", string);
    }

}
